/* 
给定一个字符串,找最长连续子串,子串只能包含英文字母,数字和下划线
dwd_sa_order_数据转换 输出dwd_sa_order_
*/

const str = "!dwd_sa_order_数据转换";
function find(str) {
  let left = 0;
  let right = 0;
  let maxLength = 0;
  let newStart = 0;
  const regex = /[A-Za-z0-9_]/;
  function isVaild(ch) {
    return regex.test(ch);
  }
  while (right < str.length) {
    if (isVaild(str[right])) {
      right++;
    } else {
      if (right - left > maxLength) {
        maxLength = right - left;
        newStart = left;
      }
      right++;
      left = right;
    }
  }
  if (right - left > maxLength) {
    maxLength = right - left;
    newStart = left;
  }
  return str.slice(newStart, newStart + maxLength);
}
console.log(find(str));
